//
//  AppDelegate.m
//  TY-C
//
//  Created by xyyl on 2024/2/29.
//

#import "AppDelegate.h"
#import "Login_BtConnetViewController.h"
#import "AiBaseNavigationController.h"
#import <IQKeyboardManager.h>
#import "People+CoreDataClass.h"
#import "ChufangModel.h"
#import "TY_DataCommand.h"


@interface AppDelegate ()

@end

@implementation AppDelegate
@synthesize window = _window; ///新版xcode特性。。


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc]init];
    self.window.frame = [UIScreen mainScreen].bounds;
    self.window.backgroundColor = [UIColor whiteColor];
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    
    
//    AiBaseTabBarController * aibase = [[AiBaseTabBarController alloc]init];
//    self.baseTabBar = aibase;
//    AiLunchViewController * lunVC  = [[AiLunchViewController alloc]init];
    Login_BtConnetViewController * homeVC = [[Login_BtConnetViewController alloc]init];
    AiBaseNavigationController * nc = [[AiBaseNavigationController alloc]initWithRootViewController:homeVC];
    nc.navigationBar.hidden = YES;
    [self checkNewApp];
    
    self.window.rootViewController = nc;
    [self.window makeKeyAndVisible];
    return YES;
}


- (void)checkNewApp {
    ///判断是否第一次登录app
    
    NSString * str =  [[NSUserDefaults standardUserDefaults] objectForKey:@"isNew"];
    if (str.length > 1) {
        [TY_DataCommand checkAllData];
    }else{
        [[NSUserDefaults standardUserDefaults] setObject:@"222" forKey:@"isNew"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        [self setLocalData];
    }
    
}

- (void)setLocalData {
    ///持续时间，间隔时间，治疗时间，波升，波降，频率，脉宽
    NSArray * aaa = @[@"4.0", @"12.0", @"30", @"2.0", @"0.0", @"50", @"200"];
    NSArray * bbb = @[@"4.0", @"12.0", @"30", @"2.0", @"0.0", @"50", @"450"];
    NSArray * ccc = @[@"4.0", @"12.0", @"30", @"2.0", @"0.0", @"50", @"150"];

    NSArray * ddd = @[@"10.0", @"10.0", @"30", @"2.0", @"0.0", @"35", @"250"];
    NSArray * ttt = @[aaa,bbb,ccc,ddd];

    for (int i = 0; i<4; i++) {
        ChufangModel * pp = [[ChufangModel alloc]init];
        pp.isfscs = @"0";
        pp.isbj = @"0";
        pp.isdel = @"0";
        pp.cfmc = [NSString stringWithFormat:@"神经刺激方案%ld",i+1];
        pp.cflx = @"NMES";
        NSArray * nnn = ttt[i];
        pp.pl = nnn[5];
        pp.mk = nnn[6];
        pp.zlsj = nnn[2];
        pp.cxsj = nnn[0];
        pp.jgsj = nnn[1];
        pp.sssj = nnn[3];
        pp.xjsj = nnn[4];
        pp.ycsj = @"-";
        
        pp.creatTime = [NSString getNowTimeStr];
        pp.uploadTime = [NSString getNowTimeStr];
        [TY_DataCommand addDataByPeopleModel:pp];
        
        
        
    }
}

//#pragma mark - UISceneSession lifecycle
//
//
//- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
//    // Called when a new scene session is being created.
//    // Use this method to select a configuration to create the new scene with.
//    return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
//}
//
//
//- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
//    // Called when the user discards a scene session.
//    // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
//    // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
//}


#pragma mark - Core Data stack

@synthesize persistentContainer = _persistentContainer;


- (NSPersistentContainer *)persistentContainer {
    // The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
    @synchronized (self) {
        if (_persistentContainer == nil) {
            _persistentContainer = [[NSPersistentContainer alloc] initWithName:@"TY_C"];
            [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
                
                if (error != nil) {
                    // Replace this implementation with code to handle the error appropriately.
                    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                    
                    /*
                     Typical reasons for an error here include:
                     * The parent directory does not exist, cannot be created, or disallows writing.
                     * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                     * The device is out of space.
                     * The store could not be migrated to the current model version.
                     Check the error message to determine what the actual problem was.
                    */
                    NSLog(@"Unresolved error %@, %@", error, error.userInfo);
                    abort();
                }
                
                
                
                
            }];

            
        }
    }
    
    return _persistentContainer;
}

#pragma mark - Core Data Saving support

- (void)saveContext {
    NSManagedObjectContext *context = self.persistentContainer.viewContext;
    NSError *error = nil;
    if ([context hasChanges] && ![context save:&error]) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, error.userInfo);
        abort();
    }
}

@end
